﻿using Data;
using Data.Tables;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web.Mvc;

namespace PayrollExpress.Helpers
{
    public class BaseHelper
    {
        public const string PasswordDisplayText = "* * * * * *";

        #region enums

        public enum ControllerName
        {
            Home,
            Administration,
            Masterfile,
            Workspace,
            Report
        }

        public enum Page
        {
            [Description("Index")]
            Index = 0,

            #region Administration

            [Description("Users")]
            Users = 1,
            [Description("Employment Types")]
            EmploymentTypes = 2,
            [Description("Recurring Holidays")]
            RecurringHolidays = 3,
            [Description("Work Locations")]
            WorkLocations = 4,

            #endregion

            #region Masterfile

            [Description("Holidays")]
            Holidays = 5,
            [Description("Employees")]
            Employees = 6,
            [Description("Employee Contracts")]
            EmployeeContracts = 7,

            #endregion

            #region Workspace

            [Description("Payroll")]
            Payroll = 8,

            [Description("Payroll Details")]
            PayrollDetails = 9,

            [Description("Generate Payroll Details")]
            GeneratePayrollDetails = 10,

            #endregion

            #region
            [Description("Reports")]
            Reports = 11

            #endregion
        }

        public enum PageAddEditSave
        {
            #region Administration
            
            [Description("Add / Edit User")]
            UserAddEdit,
            [Description("Add / Edit Employment Type")]
            EmploymentTypeAddEdit,
            [Description("Add / Edit Recurring Holiday")]
            RecurringHolidayAddEdit,
            [Description("Add / Edit Work Location")]
            WorkLocationAddEdit,

            UserSave,
            EmploymentTypeSave,
            WorkLocationSave,
            RecurringHolidaySave,

            UsersRefresh,
                        
            #endregion

            #region Masterfile

            [Description("Add Holidays")]
            HolidaysAdd,
            [Description("Edit Holiday")]
            HolidayEdit,
            [Description("Add / Edit Employee")]
            EmployeeAddEdit,
            [Description("Add / Edit Employee Contract")]
            EmployeeContractAddEdit,

            HolidaysSave,
            HolidaySave,
            EmployeeSave,
            EmployeeContractSave,
            
            HolidaysRefresh,
            EmployeesRefresh,


            #endregion

            #region Workspace

            [Description("Add / Edit Payroll")]
            PayrollAddEdit,

            PayrollSave,

            PayrollRefresh

            #endregion
        }

        public enum PageAction
        {
            [Description("Add")]
            Add,
            [Description("Edit")]
            Edit,
            [Description("Save")]
            Save,
            [Description("Delete")]
            Delete,
            [Description("Cancel")]
            Cancel,
            [Description("Set as current")]
            SetContractAsCurrent
        }

        public enum UserRole
        {
            Admin = 1,
            User = 2
        }

        #endregion

        public static IEnumerable<SelectListItem> GetRoleDropDownList()
        {
            var roles = new List<SelectListItem>();
            roles.Add(new SelectListItem() { Value = ((int)BaseHelper.UserRole.Admin).ToString(), Text = BaseHelper.UserRole.Admin.ToString() });
            roles.Add(new SelectListItem() { Value = ((int)BaseHelper.UserRole.User).ToString(), Text = BaseHelper.UserRole.User.ToString() });

            return roles;
        }

        public static int GetUserWorkLocationID(DatabaseContext db, string userName)
        {
            var id = 0;
            var list = db.Users.Where(w => w.UserName == userName);

            if (list.Any())
                id = list.SingleOrDefault<User>().WorkLocationID;

            return id;
        }

        public static int GetUserRoleID(DatabaseContext db, string userName)
        {
            var id = 0;

            var list = db.Users.Where(w => w.UserName == userName);

            if (list.Any())
                id = list.SingleOrDefault<User>().RoleID;

            return id;
        }

        public static string GetRoleName(int roleID)
        {
            return ((UserRole)roleID).ToString();
        }
        
        public static UserAccessModel GetUserAccess(DatabaseContext db, string userName)
        {
            var list = db.UserPageAccesses.Where(w => w.UserName == userName);

            var model = new UserAccessModel();

            model.HasUsersAccess = list.Where(w => w.PageID == (int)BaseHelper.Page.Users).Any();
            model.HasEmploymentTypesAccess = list.Where(w => w.PageID == (int)BaseHelper.Page.EmploymentTypes).Any();
            model.HasRecurringHolidaysAccess = list.Where(w => w.PageID == (int)BaseHelper.Page.RecurringHolidays).Any();
            model.HasWorkLocationsAccess = list.Where(w => w.PageID == (int)BaseHelper.Page.WorkLocations).Any();
            model.HasAdministrationAccess = model.HasUsersAccess
                                            || model.HasEmploymentTypesAccess
                                            || model.HasRecurringHolidaysAccess
                                            || model.HasWorkLocationsAccess;

            model.HasHolidaysAccess = list.Where(w => w.PageID == (int)BaseHelper.Page.Holidays).Any();
            model.HasEmployeesAccess = list.Where(w => w.PageID == (int)BaseHelper.Page.Employees).Any();
            model.HasMasterFilesAccess = model.HasHolidaysAccess
                                            || model.HasEmployeesAccess;

            model.HasPayrollAccess = list.Where(w => w.PageID == (int)BaseHelper.Page.Payroll).Any();
            model.HasWorkspaceAccess = model.HasPayrollAccess;

            model.HasReportsAccess = list.Where(w => w.PageID == (int)BaseHelper.Page.Reports).Any();

            return model;
        }
    }
}